import Vue from 'vue'
import Router from 'vue-router'

// 动态路由
import Approvals from './modules/approvals'
import Attendances from './modules/attendances'
import Departments from './modules/departments'
import Employees from './modules/employees'
import Permission from './modules/permission'
import Salarys from './modules/salarys'
import Setting from './modules/setting'
import Social from './modules/social'

// 动态路由（有权限才能访问到的页面）
const asyncRoutes = [
  Approvals,
  Attendances,
  Departments,
  Employees,
  Permission,
  Salarys,
  Setting,
  Social
]

Vue.use(Router)

// 布局容器
import Layout from '@/layout'

// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    // 页面的默认访问地址
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // 路由模式：history hash，默认是 hash #
  // mode: 'history', // require service support
  // 页面滚动行为：默认到一个新页面的时候，滚动到页面的顶部
  scrollBehavior: () => ({ y: 0 }),
  // 路由规则: 让静态路由和动态路由先临时合并
  routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
